﻿package controllers.read;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.Demandinfo;
import rbac.models.Oplog;
import rbac.models.User;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;


public class DemandinfoController extends ApplicationController {
	
	public void index() throws Exception{
    	
	}
	
	public void shownews() throws Exception{
		
	} 
	
    public void showexpired() throws Exception{
		
	} 
    
    public View destroyAllExpiredDemandinfo() throws Exception {
		User user = (User) rbac.getCurrentUser();
		List<Demandinfo> items = Demandinfo.findAll(Demandinfo.class, "z_id = ? and valid_time < GETDATE() ", new Object[]{ user.getDepartment().getCompany_id() });
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除过期需求，内容为：" + new JsonView(items));									

		Demandinfo.execute(Demandinfo.class,"delete Demandinfo where  z_id = ? and valid_time < GETDATE() ", new Object[]{ user.getDepartment().getCompany_id() });
		
		return new JsonView("success:true");
    }
    
    
    public View updateAllExpiredDemandinfo(int day) throws Exception {
		User user = (User) rbac.getCurrentUser();
		List<Demandinfo> items = Demandinfo.findAll(Demandinfo.class, "z_id = ? and valid_time < GETDATE() ", new Object[]{ user.getDepartment().getCompany_id() });
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")更新过期需求，内容为：" + new JsonView(items));									

		Demandinfo.execute(Demandinfo.class,"update Demandinfo set editdate = getdate(),valid_time = getdate() + ? where  z_id = ? and valid_time < GETDATE() ", new Object[]{ day, user.getDepartment().getCompany_id() });
		
		return new JsonView("success:true");
    }

	public View getExpiredDemandinfoItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		String orderBy = "editdate desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "valid_time < GETDATE()";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		Object[] args = tmpArgs.toArray();
		
		long total = Demandinfo.count(Demandinfo.class, cond, args);
		List<Demandinfo> items = Demandinfo.findAll(Demandinfo.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	public View getNewDemandinfoItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		String orderBy = "editdate desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "editdate > getdate()-1";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		Object[] args = tmpArgs.toArray();
		
		long total = Demandinfo.count(Demandinfo.class, cond, args);
		List<Demandinfo> items = Demandinfo.findAll(Demandinfo.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	public View getDemandinfoItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		String orderBy = "editdate desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "1=1 and getdate() <= valid_time";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		/**
		if (t != 0){
			cond += " and (parentId = ?)";
			tmpArgs.add(t);
		}
		**/
		if (q != null){
			cond += " and (address like '%"+q+"%' or context like '%"+q+"%' ) ";			
		}
		

		Object[] args = tmpArgs.toArray();
		
		long total = Demandinfo.count(Demandinfo.class, cond, args);
		List<Demandinfo> items = Demandinfo.findAll(Demandinfo.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	
	
		
	
	
}
